package cn.laixueit.service.impl;

import cn.laixueit.mapper.BrandMapper;
import cn.laixueit.pojo.Brand;
import cn.laixueit.pojo.BrandExample;
import cn.laixueit.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> list() {
        return brandMapper.selectByExample(null);
    }

    @Override
    public void save(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public void delById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Brand> list(Integer pagesize,Integer page,String name) {
        BrandExample example = getExample(name);
        PageHelper.startPage(page,pagesize);
        List<Brand> brandList = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        return pageInfo;
    }

    @Override
    public Brand findById(Integer id) {

        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    private BrandExample getExample(String name){
        BrandExample example = new BrandExample();
        if(StringUtils.isNotBlank(name)){
            BrandExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+name+"%");
        }
        return example;
    }
}
